package com.gwm.user.controller;

import com.gwm.user.entity.TbUser;
import com.gwm.user.service.TbUserService;
import com.gwm.user.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@Api(value="/api", description="")

@RestController
public class UserController {

    @Autowired
    private TbUserService userService;



    /**
     * 校验数据是否可用
     * @param data
     * @param type
     * @return
     */
    @ApiOperation(
            value = "校验数据是否可用",
            notes = "校验数据是否可用",
            httpMethod = "GET"
    )
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUserData(@PathVariable("data") String data, @PathVariable(value = "type") Integer type) {
        return ResponseEntity.ok(userService.checkData(data, type));
    }



    /**
     * 发送短信验证码
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone){
        userService.sendCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    @ApiOperation(
            value = "app注册",
            notes = "app注册",
            httpMethod = "POST"
    )
    @PostMapping("register")
    public ResponseEntity<Void> register(TbUser user, @RequestParam("code") String code){
        userService.register(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @ApiOperation(
            value = "校验用户名和密码",
            notes = "校验用户名和密码",
            httpMethod = "GET"
    )
    @GetMapping(value = "/query",name = "根据用户名和密码查询用户")
    public ResponseEntity<UserDTO> queryUserByUsernameAndPassword(@RequestParam("username") String username, @RequestParam("password") String password){
        return ResponseEntity.ok(userService.queryUserByUsernameAndPassword(username, password));
    }





}